package com.demo1;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {

	public static void main(String[] args) throws Exception {
		//1.创建服务器端对象
		ServerSocket server = new ServerSocket(9001);
		System.out.println("服务器启动中...");
		//2.等待客户端连接
		Socket socket = server.accept();
		//3.获取客户端的IP
		String ip=socket.getInetAddress().getHostAddress();
		System.out.println(ip+"连接到服务器...");
		//4.发送到客户端提示信息
		String msg = "恭喜你连接到服务器";
		//5.获取客户端的输出流
		OutputStream out = socket.getOutputStream();
		out.write(msg.getBytes());
		out.close();
		socket.close();
		server.close();
	}

}
